package com.ruicar.afs.cloud.basic.proceeds.pay.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruicar.afs.cloud.basic.proceeds.debit.vo.DebitPayMentVO;
import com.ruicar.afs.cloud.basic.proceeds.pay.condition.PayListQueryCondition;
import com.ruicar.afs.cloud.basic.proceeds.pay.entity.BasicPaymentPool;
import com.ruicar.afs.cloud.common.modules.contract.enums.YesOrNoEnum;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: Lee
 * @date 2020-05-30 04:13:03 
 * @description
 */
public interface BasicPaymentPoolMapper extends BaseMapper<BasicPaymentPool> {

	IPage<DebitPayMentVO> queryPayMentByQueryParam(IPage page,@Param("query") PayListQueryCondition payListQueryCondition);

	/**
	 * 获取付款中状态的msgid
	 * @author ZC.GUO
	 * @param
	 * @return
	 */
	List<String> queryMsgIdList();


	/**
	 * 开始进行付款操作时锁定数据
	 * @author ZC.GUO
	 * @param var1 id
	 * @param var2 乐观锁
	 * @return
	 */
	@Update({"update basic_payment_pool set is_paying = 'yes', revision = revision + 1 where id=#{id} and revision=#{revision} and is_paying != 'yes'"})
	@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	int lockWhenStartPay( @Param("id") Long var1, @Param("revision") Long var2 );
	/**
	 * 对单条数据进行锁定或解锁
	 * @author ZC.GUO
	 * @param var1 id
	 * @param var2 动作
	 * @return
	 */
	@Update({"update basic_payment_pool set is_paying = #{action} where id=#{id} and is_paying != #{action}"})
	@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	int readySingleWhenStartPay( @Param("id") Long var1, @Param("action") YesOrNoEnum var2 );

	/**
	 * 批量付款完成后解锁
	 * @author ZC.GUO
	 * @param var1 动作，锁定和解锁（锁定yes,解锁no)
	 * @param var2 idList
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	int updatePaymentPoolStatus(@Param("action") YesOrNoEnum var1, @Param("idList") List<Long> var2);
	/**
	 * 开始进行付款操作时锁定数据
	 * @author ZC.GUO
	 * @param var1 id
	 * @param var2 乐观锁
	 * @return
	 */
	@Update({"update basic_payment_pool set is_paying = 'yes', revision = revision + 1,msgid=#{msgid} where id=#{id} and revision=#{revision} and is_paying != 'yes' and ( msgid is null or  msgid ='') "})
	@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	int lockWhenStartPayAndAddMsgId( @Param("id") Long var1, @Param("revision") Long var2, @Param("msgid") String var3);
	/**
	 * 结束付款操作时解除数据锁定
	 * @author ZC.GUO
	 * @param var1 id
	 * @param var2 乐观锁
	 * @return
	 */
	@Update({"update basic_payment_pool set is_paying = 'no', revision = revision + 1 where id=#{id} and revision=#{revision} and is_paying != 'no'"})
	@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	int unLockWhenEndPay( @Param("id") Long var1, @Param("revision") Long var2);

	/**
	 * 结束付款操作时解除数据锁定
	 * @param var1 id
	 * @return
	 */
	@Update({"update basic_payment_pool set is_paying = 'no', revision = revision + 1 where msgid=#{msgid} and is_paying != 'no'"})
	@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
	int unLockBathWhenEndPay( @Param("msgid") String var1);
}
